<template>
  <div class="container">
    <scroll-view scroll-x
                 class="top"
                 :scroll-left="navScrollLeft"
                 scroll-with-animation>
      <ul class="tabbox">
        <li class="li"
            v-for="(item, index) in tablist"
            :key="index"
            :class="{'active':currentIndex == index}"
            @tap="tabEvent(index,item.id)">{{item.name}}</li>
        <li class="item">|</li>
      </ul>
    </scroll-view>
    <div class="dv"
         :animation="animationData"></div>
    <scroll-view scroll-y
                 :style="{'height': '91%'}"
                 class="main"
                 @scroll="scroll">
      <ul class="ul clearfix"
          v-for="(item, index) in sonlist"
          :key="index">
        <p class="title">{{item.name}}</p>
        <li class="li"
            v-for="(value, num) in item.children"
            :key="num"
            @click="to_class_detail(value)">{{value.name}}</li>
      </ul>
      <div class="bgbox"
           v-if="bgMark">
        <img src="https://img.ameimeika.com/h5_images/mp_images/mp_3.0/search/nocar.png"
             alt
             class="nothing">
        <div class="text">暂无相关分类</div>
      </div>
    </scroll-view>
  </div>
</template>

<script>
import common from "@/assets/js/mmk_common.js";
export default {
  data () {
    return {
      navScrollLeft: 0, //nav移动位置
      windowHeight: 0,
      windowWidth: 0,
      tablist: [],
      animationData: {},
      currentIndex: 0,
      sonlist: [],
      bgMark: false
    };
  },
  onLoad (options) {
    wx.setNavigationBarTitle({
      title: "全部分类"
    });
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: "#F64665",
    });
    this.sonlist = [];
    wx.getSystemInfo({
      success: res => {
        this.windowHeight = res.windowHeight;
        this.windowWidth = res.windowWidth;
      }
    });
    this.get_tab();
  },
  onShow () { },
  methods: {
    get_tab () {
      wx.showLoading({
        title: '', //提示的内容,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => { }
      });
      common.fly_post(
        "/api/com.mmk.reservation.api.OpenBeautyCategoriesProvider/1.0.0/oneSearch.html",
        {},
        msg => {
          wx.hideLoading();
          let res = msg.data;
          if (res.code == 0) {
            this.tablist = res.data;
            this.tabEvent(0, res.data[0].id);
          } else {
            wx.showToast({
              title: res.msg, //提示的内容,
              icon: 'none', //图标,
              duration: 1500, //延迟时间,
              mask: false, //显示透明蒙层，防止触摸穿透,
              success: res => { }
            });
          }
        }, "isJava"
      );
    },
    tabEvent (index, e) {
      this.bgMark = false;
      let cur = index;
      let singleNavWidth = this.windowWidth / 5;
      this.navScrollLeft = (cur - 3) * singleNavWidth;
      if (this.currentIndex == cur) {
        // return false;
      } else {
        this.currentIndex = cur;
      }
      // this.currentIndex = index;  //tab栏效果
      wx.showLoading({
        title: '', //提示的内容,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => { }
      });
      common.fly_post(
        "/api/com.mmk.reservation.api.OpenBeautyCategoriesProvider/1.0.0/twoSearch.html",
        { id: e },
        msg => {
          wx.hideLoading();
          let res = msg.data;
          if (res.code == 0) {
            this.sonlist = res.data;
            if (res.data.length == 0) {
              this.bgMark = true;
            } else {
              this.bgMark = false;
            }
          } else {
            wx.showToast({
              title: res.msg, //提示的内容,
              icon: 'none', //图标,
              duration: 1500, //延迟时间,
              mask: false, //显示透明蒙层，防止触摸穿透,
              success: res => { }
            });
          }
        }, "isJava"
      );
    },
    scroll (e) {
      //滚动触发动画
      let top = e.mp.detail.scrollTop - 0;
      //  console.log(e)
      if (top++) {
        this.slideupshow(0, 0);
      } else if (top--) {
        this.slideupshow(35, 1);
      }
    },
    slideupshow (px, opacity) {
      //动画效果
      var animation = wx.createAnimation({
        duration: 500,
        timingFunction: "ease"
      });
      animation
        .height(px)
        .opacity(opacity)
        .step();
      //设置动画
      this.animationData = animation.export();
    },
    to_class_detail (item) {
      switch (item.type) {
        case 0:
          // 全部分类
          wx.navigateTo({ url: "/pages/package_projects/total_classification/main" });
          break;
        case 1:
          // 一级分类
          wx.navigateTo({
            url: `/pages/package_projects/two_level/main?obj=${JSON.stringify(item)}`
          });
          break;
        case 2:
          // 二级分类
          wx.navigateTo({
            url: `/pages/package_projects/two_level/main?obj=${JSON.stringify(item)}`
          });
          break;
        case 3:
          // 三级分类
          wx.navigateTo({
            url: `/pages/package_projects/three_level/main?cate_name=${item.name}&cate_id=${
              item.id
              }`
          });
          break;

        default:
          break;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #f3f4f6;
  position: relative;
  .top {
    background: linear-gradient(360deg, #f97b8f 0%, #f64665 100%);
    position: -webkit-sticky;
    position: -moz-sticky;
    position: sticky;
    z-index: 99;
    .tabbox {
      height: 50px;
      box-sizing: content-box;
      padding: 0 15px;
      display: flex;
      white-space: nowrap;
      background: linear-gradient(360deg, #f97b8f 0%, #f64665 100%);
      .item {
        width: 1px;
        height: 40px;
        color: transparent;
      }
      .li {
        float: left;
        width: auto;
        height: 36px;
        margin-right: 19px;
        color: #fff;
        font-size: 14px;
        line-height: 40px;
        &.li:last-of-type {
          margin-right: 0;
        }
        &.active {
          position: relative;
          font-weight: bold;
          &::before {
            content: "";
            width: 100%;
            height: 4px;
            background: #fff;
            border-radius: 2px;
            position: absolute;
            bottom: 5%;
          }
        }
      }
    }
  }
  .dv {
    width: 100%;
    height: 35px;
    background: linear-gradient(360deg, #fa7c90 0%, #f97b8f 100%);
    display: block;
  }
  .main {
    width: 92%;
    position: absolute;
    top: 50px;
    left: 4%;
    background-color: #fff;
    border-radius: 4px;
    padding: 0;
    z-index: 1;
    .ul {
      height: auto;
      color: #666;
      padding: 15px 2px 10px 10px;
      margin: 0 auto;
      box-sizing: border-box;
      clear: both;
      .li {
        width: auto;
        height: 32px;
        float: left;
        background: rgba(246, 70, 101, 0.1);
        border-radius: 4px;
        line-height: 32px;
        margin: 0 8px 8px 0;
        text-align: center;
        position: relative;
        color: #666;
        // overflow: hidden;
        // text-overflow: ellipsis;
        // display: -webkit-box;
        // -webkit-line-clamp: 1;
        // -webkit-box-orient: vertical;
        padding: 0 9px;
        box-sizing: border-box;
      }
      .title {
        color: #333;
        font-size: 15px;
        text-align: center;
        margin-bottom: 15px;
        position: relative;
        &::before {
          content: "";
          width: 96%;
          height: 1px;
          background: #f3f4f6;
          // border-radius:2px;
          position: absolute;
          top: -78%;
          left: 1%;
        }
      }
    }
    .bgbox {
      width: 100%;
      height: 100%;
      padding-top: 97px;
      box-sizing: border-box;
      .nothing {
        // width: 100px;
        // height: 90px;
        width: 128px;
        height: 114px;
        margin: 0 auto;
        display: block;
      }
      .text {
        width: 100%;
        font-size: 14px;
        font-weight: 400;
        margin-top: 13px;
        color: rgba(153, 153, 153, 1);
        text-align: center;
      }
    }
  }
}
</style>
